package com.dyh.quickdemozwei.t0907;

import com.google.common.collect.Lists;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class T0907 {


    @Test
    public void test4() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        System.out.println(sdf.parse("2022/9/12"));
    }


    static class ListTraverse{

        private List list;
        private List<Object> consumers;

        public void setList(List list) {
            this.list = list;
        }

        public void setConsumers(List<Object> consumers) {
            this.consumers = consumers;
        }

        private ListTraverse(){}



        public static class TraverseContext{
            int index;

            boolean orderBreak;
            boolean orderContinue;

            public void orderBreak() {
                this.orderBreak = true;
            }

            public void orderContinue() {
                this.orderContinue = true;
            }
        }





        public void work(){

            TraverseContext traverseContext = new TraverseContext();

            l1:
            for (traverseContext.index=0 ;traverseContext.index<list.size();traverseContext.index++){

                for (Object consumer : consumers) {
                    if (consumer instanceof Consumer)
                        ((Consumer) consumer).accept(list.get(traverseContext.index));
                    if (consumer instanceof BiConsumer)
                        ((BiConsumer) consumer).accept(traverseContext,list.get(traverseContext.index));

                    if (traverseContext.orderBreak)
                        break l1;
                    if (traverseContext.orderContinue)
                        continue l1;
                }
            }


        }
    }

    static class ListTraverseBootstrap<T>{
        private List<T> list;
        private List<Object> consumers;

        private ListTraverseBootstrap(){}

        public static <T> ListTraverseBootstrap<T> from(List<T> list){
            ListTraverseBootstrap<T> listTraverse = new ListTraverseBootstrap<>();
            listTraverse.list = list;
            listTraverse.consumers = new ArrayList<>();
            return listTraverse;
        }



        public ListTraverseBootstrap<T> addConsumer(Consumer<T> consumer){
            consumers.add(consumer);
            return this;
        }

        public ListTraverseBootstrap<T> addAdvanceConsumer(BiConsumer<ListTraverse.TraverseContext,T> consumer){
            consumers.add(consumer);
            return this;
        }

        public ListTraverse build(){
            ListTraverse listTraverse = new ListTraverse();
            listTraverse.setList(list);
            listTraverse.setConsumers(consumers);
            return listTraverse;
        }
    }





    @Test public void test3(){
        List<Integer> list = Lists.newArrayList(1,2,3,4,5);

        ListTraverseBootstrap.from(list)
                .addConsumer(e-> System.out.println("c1 "+e))
                .addAdvanceConsumer((ctx,e)->{
                    if (e==4)
                        ctx.orderBreak();
//                        ctx.orderContinue();
                    else
                        System.out.println("c2 "+e);
                })
                .addConsumer(e-> System.out.println("c3 "+e))
        .build().work();
        ;


        System.out.println();

    }

    @Test
    public void test(){

        int i1 = 1;
        long l1 = 100L;
        String s1 = "xx";

        for (int i = 0;i<5;i++){
            xxx(i1, l1, s1, i);

            System.out.println(i*i1);
        }
    }

    private void xxx(int i1, long l1, String s1, int i) {
        System.out.println(i1 * l1 + i + s1);
    }


    @Test
    public void test1(){

        int i1 = 1;
        long l1 = 100L;
        String s1 = "xx";



        for (int i = 0;i<5;i++){

            int finalI = i;
            Runnable r1 = ()->{
                xxx(i1, l1, s1, finalI);
            };


            Runnable r2 = ()->{
                System.out.println(finalI *i1);
            };

            r1.run();
            r2.run();
        }
    }


    @Test
    public void test2(){

        int i1 = 1;
        long l1 = 100L;
        String s1 = "xx";



        for (int i = 0;i<5;i++){

            int finalI = i;
            {
                xxx(i1, l1, s1, finalI);
                //dawd

//                dawdawd
            }


            {
                System.out.println(finalI *i1);
            }

        }
    }
}
